unit DBDateTimePicker;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls,DB,dbctrls;

type
TDBDateTimePicker = class(TDateTimePicker)
private
{ Private declarations }
FFieldDataLink:TFieldDataLink;
function GetReadOnly:boolean;
procedure SetReadOnly(Value:boolean);
function GetDataField:string;
procedure SetDataField(Value:string);
function GetDataSource:TDataSource;
procedure SetDataSource(Value:TDataSource);
function GetField:TField;
procedure CNNotify(var Message: TWMNotify); message CN_NOTIFY;
procedure CMExit(var Message:TCMExit);message CM_EXIT;
protected
{ Protected declarations }
procedure FDataChange(Sender:TObject);
procedure FUpdateData(Sender:TObject);
procedure Notification(AComponent: TComponent;
Operation: TOperation); override;
public
{ Public declarations }
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
property Field:TField read GetField;
published
{ Published declarations }
property Anchors;
property BiDiMode;
property CalAlignment;
property CalColors;
property Constraints;
property DataSource:TDataSource read GetDataSource write SetDataSource;
property DataField:string read GetDataField write SetDataField;
property ReadOnly:boolean read GetReadOnly write SetReadOnly;
// The Date, Time, ShowCheckbox, and Checked properties must be in this order:
property ShowCheckbox;
property Checked;
property Color;
property DateFormat;
property DateMode;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property ImeMode;
property ImeName;
property Kind;
property MaxDate;
property MinDate;
property ParseInput;
property ParentBiDiMode;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop;
property Visible;
property OnClick;
property OnCloseUp;
property OnChange;
property OnDropDown;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnKeyDown;
property OnKeyPress;
property OnKeyUp;
property OnStartDock;
property OnStartDrag;
property OnUserInput;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Data Controls', [TDBDateTimePicker]);
end;

{ TDBDateTimePicker }

procedure TDBDateTimePicker.CMExit(var Message: TCMExit);
begin
try
FFieldDataLink.UpdateRecord;
except
Self.SetFocus;
raise;
end;
inherited;
end;

procedure TDBDateTimePicker.CNNotify(var Message: TWMNotify);
begin
FFieldDataLink.Edit;
inherited;
FFieldDataLink.Modified;
end;

constructor TDBDateTimePicker.Create(AOwner: TComponent);
begin
inherited Create(AOwner);

FFieldDataLink:=TFieldDataLink.Create;
FFieldDataLink.ReadOnly:=False;
FFieldDataLink.Control:=Self;
FFieldDataLink.OnDataChange:=FDataChange;
FFieldDataLink.OnUpdateData:=FUpdateData;
end;

destructor TDBDateTimePicker.Destroy;
begin
FFieldDataLink.Free;
FFieldDataLink:=nil;

inherited;
end;

procedure TDBDateTimePicker.FDataChange(Sender: TObject);
begin
if (FFieldDataLink<>nil) and (FFieldDataLink.Field<>nil) then
begin
Self.Date:=Trunc(FFieldDataLink.Field.AsDateTime);
Self.Time:=Frac(FFieldDataLink.Field.AsDateTime);
end;
end;

procedure TDBDateTimePicker.FUpdateData(Sender: TObject);
begin
if (FFieldDataLink<>nil) and (FFieldDataLink.Field<>nil) then
FFieldDataLink.Field.AsDateTime:=TDateTime(Date);
end;

function TDBDateTimePicker.GetDataField: string;
begin
Result:=FFieldDataLink.FieldName;
end;

function TDBDateTimePicker.GetDataSource: TDataSource;
begin
Result:=FFieldDataLink.DataSource;
end;

function TDBDateTimePicker.GetField: TField;
begin
Result:=FFieldDataLink.Field;
end;

function TDBDateTimePicker.GetReadOnly: boolean;
begin
Result:=FFieldDataLink.ReadOnly;
end;

procedure TDBDateTimePicker.Notification(AComponent: TComponent;
Operation: TOperation);
begin
inherited Notification(AComponent,Operation);
if (Operation=opReMove) and (FFieldDataLink<>nil) and
(AComponent=FFieldDataLink.DataSource) then
FFieldDataLink.DataSource:=nil;
end;

procedure TDBDateTimePicker.SetDataField(Value: string);
begin
FFieldDataLink.FieldName:=Value;
end;

procedure TDBDateTimePicker.SetDataSource(Value: TDataSource);
begin
FFieldDataLink.DataSource:=Value;
if Value<>nil then Value.FreeNotification(Self);
end;

procedure TDBDateTimePicker.SetReadOnly(Value: boolean);
begin
FFieldDataLink.ReadOnly:=Value;
end;

end.

